Imports System.Windows.Forms
Imports Vintasoft.Pdf.Security

Namespace Pdf.Security
	Public Partial Class SecuritySettingsForm
		Inherits Form
		Public Sub New(currentEncryptionSettings As EncryptionSystem)
			InitializeComponent()
			If currentEncryptionSettings Is Nothing Then
				securityMethodComboBox.Items(0) = "Don't Change (No Security)"
			Else
				securityMethodComboBox.Items(0) = String.Format("Don't Change ({0})", currentEncryptionSettings)
			End If
			securityMethodComboBox.SelectedIndex = 0
			_newEncryptionSettings = currentEncryptionSettings
		End Sub

		Private _newEncryptionSettings As EncryptionSystem
		Public ReadOnly Property NewEncryptionSettings() As EncryptionSystem
			Get
				Return _newEncryptionSettings
			End Get
		End Property

		Private Sub printInLowQualityCheckBox_CheckedChanged(sender As Object, e As EventArgs)
			printInHighQualityCheckBox.Enabled = printInLowQualityCheckBox.Checked
			If Not printInHighQualityCheckBox.Enabled Then
				printInHighQualityCheckBox.Checked = False
			End If
		End Sub

		Private Sub buttonOk_Click(sender As Object, e As EventArgs)
			Try
				If securityMethodComboBox.SelectedIndex = 1 Then
					_newEncryptionSettings = Nothing
				ElseIf securityMethodComboBox.SelectedIndex > 1 Then
					Dim accessPermissions As UserAccessPermissions = UserAccessPermissions.None
					If printInLowQualityCheckBox.Checked Then
						accessPermissions = accessPermissions Or UserAccessPermissions.PrintDocumentInLowResolution
					End If
					If printInHighQualityCheckBox.Checked Then
						accessPermissions = accessPermissions Or UserAccessPermissions.PrintDocumentInHighResolution
					End If
					If extractTextAndGraphicsCheckBox.Checked Then
						accessPermissions = accessPermissions Or UserAccessPermissions.ExtractTextAndGraphics
					End If
					If modifyContentsCheckBox.Checked Then
						accessPermissions = accessPermissions Or UserAccessPermissions.ModifyContents
					End If
					If modifyAnnotationsCheckBox.Checked Then
						accessPermissions = accessPermissions Or UserAccessPermissions.ModifyAnnotations
					End If
					If fillInteractiveFormFieldsCheckBox.Checked Then
						accessPermissions = accessPermissions Or UserAccessPermissions.FillInteractiveFormFields
					End If
					If assembleDocumentCheckBox.Checked Then
						accessPermissions = accessPermissions Or UserAccessPermissions.AssembleDocument
					End If

					Dim keyLength As Integer
					If securityMethodComboBox.SelectedIndex = 2 Then
						keyLength = 40
					Else
						keyLength = 128
					End If

					Dim algorithm As EncryptionAlgorithm
					If securityMethodComboBox.SelectedIndex <= 3 Then
						algorithm = EncryptionAlgorithm.RC4
					Else
						algorithm = EncryptionAlgorithm.AES
					End If

					_newEncryptionSettings = New EncryptionSystem(algorithm, keyLength, userPasswordTextBox.Text, ownerPasswordTextBox.Text, accessPermissions)
				End If
				DialogResult = DialogResult.OK
			Catch ex As Exception
				DemosTools.ShowErrorMessage(ex)
			End Try
		End Sub

		Private Sub buttonCancel_Click(sender As Object, e As EventArgs)
			DialogResult = DialogResult.Cancel
		End Sub

		Private Sub showPasswordsCheckBox_CheckedChanged(sender As Object, e As EventArgs)
			If showPasswordsCheckBox.Checked Then
				userPasswordTextBox.PasswordChar = ControlChars.NullChar
				ownerPasswordTextBox.PasswordChar = ControlChars.NullChar
			Else
				userPasswordTextBox.PasswordChar = "*"C
				ownerPasswordTextBox.PasswordChar = "*"C
			End If
		End Sub

		Private Sub securityMethodComboBox_SelectedIndexChanged(sender As Object, e As EventArgs)
			Dim setNewSecurityMethod As Boolean = securityMethodComboBox.SelectedIndex > 1
			passwordsGroupBox.Enabled = setNewSecurityMethod
			permissionsGroupBox.Enabled = setNewSecurityMethod
		End Sub

	End Class
End Namespace
